
$(function() {
	

	// 隐藏导航跟底部
	var windowheight = $(window).height();
	var margintop = (windowheight-$(".loginPublic").height())/2-55;
	$(".site-footer").css("display","none");
	$(".header").css("display","none");
	$(".loginPublic").css("margin-top",margintop);
	
	/*登录提交*/
	$(document).on( "click", ".sublogo1", function(e) {
		submitForm();
		stopDefault(e);
	});

	/* 按enter提交表单 */
	document.onkeydown = function(e) {
		var e = event || window.event || arguments.callee.caller.arguments[0],
		 keycode = e.which || e.keyCode;
		 if (keycode==13) {
		  	submitForm();
		  	return false; 
		 }else{
		 	return true;
		 }
		 stopDefault(e);
	}

	$('#username').bind('input propertychange', function(e) {
			$(".txt").hide();
			$(".error-tip").hide();
			$('.rphonecode').unbind('input propertychange');
			stopDefault(e);
	});
	$('#password').bind('input propertychange', function(e) {
			$(".txt").hide();
			$(".error-tip").hide();
			$('.rphonecode').unbind('input propertychange');
			stopDefault(e);
	});

});

function submitForm(){
	var username = $(".username");
	var password = $(".password");
	var post_id =  JSON.parse(localStorage.getItem('post_id'));
	console.info(post_id);
	$("#post_id").attr("value",post_id);
	if(checkusername(username) && checkPWd(password)){
		var loading = layer.msg('加载中...', {icon: 16});
		var login = $(".deform");
		var username = $("#username").val();
      	var password = $("#password").val();
		$.post(jumpUrl+"/login", {"password":password,"username":username,"post_id":post_id},function(data){
			var token = data.token;
			localStorage.setItem("token", JSON.stringify(token));
			if(post_id != null){
				var post_id_status = true;
				localStorage.setItem("post_id_status", JSON.stringify(post_id_status));
			}
			var goback = sessionStorage.getItem("goback");
			if(goback == null || goback==""  || goback=="undefined" || goback.indexOf(jumpUrl + '/login') > -1){
					window.location.href=jumpUrl;
			}else{
    			location.href= goback;
			}
			sessionStorage.removeItem("goback");
			
		}).error(function(data) {
			   var obj = $.parseJSON(data.responseText);
			   layer.close(loading);
			   $(".error-tip").show();
			   $(".error-tip").text(obj.error_messages.password);
		});
		   return true;   
	}else{
		return false;
	}
}
